// by Xeno
#define THIS_FILE "fn_bike.sqf"
#include "x_setup.sqf"
private ["_create_bike", "_disp_name", "_pos", "_vehicle", "_exitit", "_dosearch", "_index", "_parray", "_rank","_water"];
if (!X_Client) exitWith {};

_create_bike = (_this select 3) select 0;
_b_mode = (_this select 3) select 1;

_exitit = false;
if (GVAR(with_ranked)) then {
	_dosearch = true;
	if ((_create_bike in GVAR(create_bike)) && {count GVAR(create_bike) > 1}) then {
		_index = GVAR(create_bike) find _create_bike;
		if (_index != -1) then {
			_dosearch = false;
			_parray = [GVAR(points_needed) select 1, GVAR(points_needed) select 2, GVAR(points_needed) select 3, GVAR(points_needed) select 4, GVAR(points_needed) select 5];
			if (_index < count _parray && {score player < (_parray select _index)}) then {
				_rank = switch (_parray select _index) do {
					case (GVAR(points_needed) select 1): {"Sergeant"};
					case (GVAR(points_needed) select 2): {"Lieutenant"};
					case (GVAR(points_needed) select 3): {"Captain"};
					case (GVAR(points_needed) select 4): {"Major"};
					case (GVAR(points_needed) select 5): {"Colonel"};
				};
				systemChat format [(localize "STR_DOM_MISSIONSTRING_156"),_rank,_create_bike];
				_exitit = true;
			};
		};
	};
	if (_dosearch && {score player < (GVAR(ranked_a) select 6)}) then {
		_rank = (GVAR(ranked_a) select 6) call FUNC(GetRankFromScore); 
		_exitit = true;
		systemChat format[(localize "STR_DOM_MISSIONSTRING_157"),_create_bike,_rank];
	};
};
// TYRGHEN - Added boat check
if (_create_bike isKindOf "Ship") then {
	_b_mode = 1;
	_water = [getPos player, 40] call FUNC(findWater);
	if (count _water <= 0) then {
		_exitit = true;
		systemChat format [(localize "STR_DOM_MISSIONSTRING_2039"), 40];
	};
};
if (_exitit) exitWith {};

_disp_name = [_create_bike,0] call FUNC(GetDisplayName);

if (vehicle player != player) exitWith {
	systemChat format [(localize "STR_DOM_MISSIONSTRING_158"), _disp_name];
};

if (isNil {__pGetVar(GVAR(bike_created))}) then {__pSetVar [QGVAR(bike_created), false]};
if (_b_mode == 1 && {__pGetVar(GVAR(bike_created))}) exitWith {systemChat (localize "STR_DOM_MISSIONSTRING_159")};

if (diag_tickTime > GVAR(vec_end_time) && {!isNull GVAR(flag_vec)} && {(GVAR(flag_vec) call FUNC(GetVehicleEmpty))}) then {
	deleteVehicle GVAR(flag_vec);
	GVAR(flag_vec) = objNull;
	GVAR(vec_end_time) = -1;
};
if (_b_mode == 0 && {alive GVAR(flag_vec)}) exitWith {
	systemChat format [(localize "STR_DOM_MISSIONSTRING_160"),0 max (round((GVAR(vec_end_time) - diag_tickTime)/60))];
};

if (GVAR(with_ranked)) then {
	[QGVAR(pas), [player, (GVAR(ranked_a) select 5) * -1]] call FUNC(NetCallEventCTS);
};

systemChat format [(localize "STR_DOM_MISSIONSTRING_161"), _disp_name];
sleep 3.123;
__pSetVar [QGVAR(bike_b_mode), _b_mode];
_vet = if (_b_mode == 1) then {-1} else {
	GVAR(vec_end_time) = diag_tickTime + GVAR(VecCreateWaitTime) + 60;
	GVAR(vec_end_time)
};
[QGVAR(crbike), [player, _create_bike, _b_mode, _vet]] call FUNC(NetCallEventCTS);
__pSetVar [QGVAR(bike_created), true];

